package com.cloudkinto.service.sc.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.ScFarawayInfoRuleDao;
import com.cloudkinto.entity.ScFarawayInfoRuleDo;
import com.cloudkinto.service.sc.ScFarawayInfoRuleService;
import com.cloudkinto.vo.sc.ScFarawayInfoPageVo;
import com.cloudkinto.vo.sc.faraway.FarawayInfoRuleAddReq;
import com.cloudkinto.vo.sc.faraway.FarawayInfoRuleUpdateReq;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author hua
 * @since 2024-11-05
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ScFarawayInfoRuleServiceImpl extends ServiceImpl<ScFarawayInfoRuleDao, ScFarawayInfoRuleDo> implements ScFarawayInfoRuleService {
    @Autowired
    private ScFarawayInfoRuleDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(FarawayInfoRuleAddReq req, Long userId, Long tenantId) {
//        ScFarawayInfoRuleDo entityDo = new ScFarawayInfoRuleDo();
//        BeanUtils.copyProperties(req, entityDo);
//        entityDo.setCreateBy(userId);
//        dao.insert(entityDo);
        return null;
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(ScFarawayInfoRuleDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(ScFarawayInfoRuleDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(FarawayInfoRuleUpdateReq req, Long userId) {
//        ScFarawayInfoRuleDo entityDo = dao.selectById(req.getId());
//        BeanUtils.copyProperties(req, entityDo);
//        dao.updateById(entityDo);
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(ScFarawayInfoRuleDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(ScFarawayInfoRuleDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        int i = dao.updateDelete(id, userId);
        return i;
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(ScFarawayInfoRuleDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        ScFarawayInfoRuleDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<ScFarawayInfoPageVo> getList(Map<String, Object> map) {
        List<ScFarawayInfoPageVo> list = dao.list(map);
        return list;
    }

    @Override
    public PageResult getListPage(Map<String, Object> map) {
        IPage<ScFarawayInfoPageVo> page = pageInitDto(map);
        page = dao.list(page, map);
        PageResult result = new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<Object> handlerListAfter(List<ScFarawayInfoRuleDo> list) {
        List<Object> result = new ArrayList<>();
        return result;
    }

    private IPage<ScFarawayInfoRuleDo> pageInit(Map<String, String> map) {
        IPage<ScFarawayInfoRuleDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private IPage<ScFarawayInfoPageVo> pageInitDto(Map<String, Object> map) {
        IPage<ScFarawayInfoPageVo> page = new Page();
        page.setSize(map.get("pageSize") != null ? Long.parseLong(map.get("pageSize").toString()) : 10);
        page.setCurrent(map.get("current") != null ? Long.parseLong(map.get("current").toString()) : 1);
        return page;
    }

    private QueryWrapper<ScFarawayInfoRuleDo> queryBuild(Map<String, String> map) {
        QueryWrapper<ScFarawayInfoRuleDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


}
